package com.bcx.crm.spring.beans.dynamicRegistryBean;

import com.bcx.crm.spring.aop.Artist;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * Created by ys on 2018/1/5
 * spring 实现动态注册 Bean
 * 实现了BeanDefinitionRegistryPostProcessor的类可以获得BeanDefinitionRegistry 对象，通过它可以动态的注册组件
 */
@Component
public class DynamicRegistryBean implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        GenericBeanDefinition definition = new GenericBeanDefinition();
        definition.setBeanClass(Artist.class);   //设置类
        definition.setScope("singleton");   //设置scope
        definition.setLazyInit(false);    //设置是否懒加载
        definition.setAutowireCandidate(true);  //设置是否可以被其他对象自动注入
        beanDefinitionRegistry.registerBeanDefinition("artist",definition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {

    }
}
